// 安装 web-storage-cache 插件
// 特点 存储时自动将对象转为json
//      获取时自动将json转为对象
import Storage from 'web-storage-cache'

const localStorage = new Storage()

export function setLocalStorage(key, value) {
    // 通过 localStorage.set 写入数据
    return localStorage.set(key, value)
}

export function getLocalStorage(key) {
    // 获取数据
    return localStorage.get(key)
}

export function removeLocalStorage(key) {
    // 删除数据
    return localStorage.delete(key)
}

export function clearLocalStorage() {
    // 清空数据
    return localStorage.clear()
}

// 每一本电子书的设置都可能不一样
// 定义setBookObject方法
// 为每一本电子书开一个localStorage空间
export function setBookObject(fileName, key, value) {
    let book = getLocalStorage(`${fileName}-info`)
    // if (book) {
    //     boo[key] = value
    // } else {
    //     book = {}
    //     book[key] = value
    // }
    if(!book) {
        book = {}
    }
    book[key] = value
    setLocalStorage(`${fileName}-info`, book)
}

// 获取存储在localStorage中的键值对
export function getBookObject(fileName, key) {
    let book = getLocalStorage(`${fileName}-info`)
    if (book) {
        return book[key]
    } else {
        return null
    }
}

// 获取字体
export function getFontFamily(fileName) {
    return getBookObject(fileName, 'fontFamily')
}
// 存储字体
export function saveFontFamily(fileName, font) {
    return setBookObject(fileName, 'fontFamily', font)
}